package com.exemplo.flickrsocial.view;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.exemplo.flickrsocial.R;
import com.exemplo.flickrsocial.model.FlickrSocialModel;
import com.exemplo.flickrsocial.service.GetOAuthTokenTask;
import com.exemplo.flickrsocial.service.LoadPhotostreamTask;
import com.exemplo.flickrsocial.service.LoadUserTask;
import com.exemplo.flickrsocial.service.OAuthTask;
import com.exemplo.flickrsocial.util.StringUtils;
import com.gmail.yuyang226.flickr.FlickrException;
import com.gmail.yuyang226.flickr.oauth.OAuth;
import com.gmail.yuyang226.flickr.people.User;

public class FlickrSocial extends Activity implements OnClickListener {

	private ListView listView;
	private TextView textUserTitle;
	private TextView textUserName;
	private TextView textUserId;
	private ImageView userIcon;
	private ImageButton refreshButton;
	private FlickrSocialModel flickrModel;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		this.flickrModel = new FlickrSocialModel(getApplicationContext());

		initiViewComponet();
		initiPreferences();

		this.refreshButton.setOnClickListener(this);

		OAuth oauth = flickrModel.getOAuthToken();
		if (oauth == null || oauth.getUser() == null) {
			OAuthTask task = new OAuthTask(this);
			task.execute();
		} else {
			load(oauth);
		}
	}

	/**
	 * initializes the components
	 */
	private void initiViewComponet() {

		this.textUserTitle = (TextView) this
				.findViewById(R.id.profilePageTitle);
		this.textUserName = (TextView) this.findViewById(R.id.userScreenName);
		this.textUserId = (TextView) this.findViewById(R.id.userId);
		this.userIcon = (ImageView) this.findViewById(R.id.userImage);
		this.listView = (ListView) this.findViewById(R.id.imageList);
		this.refreshButton = (ImageButton) this
				.findViewById(R.id.btnRefreshUserProfile);

	}

	/**
	 * Initialize the sharedPreferences.
	 */
	private void initiPreferences() {

		try {

			flickrModel.managesCreadSharedPreferences();

		} catch (FlickrException e) {
			e.printStackTrace();
		}

	}

	/**
	 * load oauth
	 * 
	 * @param oauth
	 */
	public void load(OAuth oauth) {
		if (oauth != null) {
			new LoadUserTask(this, userIcon).execute(oauth);
			new LoadPhotostreamTask(this, listView).execute(oauth);
		}

	}

	@Override
	public void onDestroy() {
		listView.setAdapter(null);
		super.onDestroy();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		setIntent(intent);
	}

	/**
	 * Set user information.
	 * 
	 * @param user
	 */
	public void setUser(User user) {
		textUserTitle.setText(user.getUsername());
		textUserName.setText(user.getRealName());
		textUserId.setText(user.getId());
	}

	public ImageView getUserIconImageView() {
		return this.userIcon;
	}

	@Override
	public void onResume() {
		super.onResume();
		Intent intent = getIntent();
		String scheme = intent.getScheme();
		OAuth savedToken = flickrModel.getOAuthToken();
		if (StringUtils.CALLBACK_SCHEME.equals(scheme)
				&& (savedToken == null || savedToken.getUser() == null)) {

			Uri uri = intent.getData();
			String query = uri.getQuery();
			Log.d(getClass().toString(), "Returned Query" + query);

			String[] data = query.split("&");

			if (data != null && data.length == 2) {
				String oauthToken = data[0].substring(data[0].indexOf("=") + 1);
				String oauthVerifier = data[1]
						.substring(data[1].indexOf("=") + 1);
				OAuth oauth = flickrModel.getOAuthToken();

				if (oauth != null && oauth.getToken() != null
						&& oauth.getToken().getOauthTokenSecret() != null) {
					GetOAuthTokenTask task = new GetOAuthTokenTask(this);
					task.execute(oauthToken, oauth.getToken()
							.getOauthTokenSecret(), oauthVerifier);

				}
			}
		}

	}

	@Override
	public void onClick(View v) {

		load(flickrModel.getOAuthToken());

	}

}